package module.building.view.buildingListPanel
{
	import com.darwin.darwinUI.assist.IGroupItem;
	import com.darwin.darwinUI.containers.Box;
	import com.darwin.darwinUI.controls.Tab;
	import com.darwin.darwinUI.controls.TabPanelBase;
	import com.darwin.darwinUI.controls.TabViewStack;
	import com.darwin.darwinUI.core.UIComponent;
	import com.darwin.darwinUI.uiconst.UIAlign;
	
	import module.building.consts.BuildConst;
	
	/**
	 * 建筑选择面板，包含两组Tabs与下方显示各个建筑项目的面板 
	 * @author hc
	 */
	public class BuildChoosePanel extends UIComponent
	{
		private var _list:BuildList;
		private var _dataArray:Array;
		private var buildTabPanel:BuildTabPanel;
		private var tabContainer:UIComponent;
		private var subTabContainer:UIComponent;
		private var tabPanelContainer:UIComponent;
		
		private var _tabPanel:TabPanelBase;
		private var _tabViewStack:TabViewStack;
		private var _autoSelectArray:Array;
		
		/**
		 * 构造方法 
		 * @param data 面板中应显示建筑的信息
		 */
		public function BuildChoosePanel(data:Array)
		{
			_dataArray = data;
			initData();
//			initDataTest();
		}
		
		private function initData():void
		{
			tabContainer = new UIComponent();
			subTabContainer = new UIComponent();
			tabPanelContainer = new UIComponent();
			_tabViewStack = new TabViewStack(subTabContainer);

			buildTabPanel = new BuildTabPanel(_dataArray);
			_list = buildTabPanel.list;
			
			for(var i:int=0;i<7;i++)
			{
				var flag:Boolean = false;
				for (var index:String in _dataArray)
				{
					if(_dataArray[index].buildingObj.type == i){flag = true;}
				}
				if(!flag){continue;}//若找不到对应type，则将其屏蔽
				
				var tab:BuildTab = new BuildTab(i);
				var subTabPanel:SubTabPanel = new SubTabPanel();
				
				var subTypeArray:Array = BuildConst.getSubType(i);
				var _subTabViewStack:TabViewStack = new TabViewStack(tabPanelContainer);
				var autoTab:SubBuildTab = new SubBuildTab(i.toString());
				subTabPanel.addChild(autoTab);
				_subTabViewStack.addBinding(buildTabPanel,autoTab);
				if(i==0){autoTab.selectAction(true);}
				tab.autoSelectSubTab = autoTab;
				for(var j:int=0;j<subTypeArray.length;j++)
				{
					var subNumber:int = 0;
					for (index in _dataArray)
					{
						if(_dataArray[index].buildingObj.type == (i+"_"+j)){subNumber++;}
					}
//					if(subNumber <= 1){continue;}//若该subType下只有1个建筑，则归类到all中
					
					var subTab:SubBuildTab = new SubBuildTab(subTypeArray[j]);
					subTab.y = 49.5;
					if(subTabPanel.numChildren > 0)
					{
						subTab.y = subTabPanel.getChildAt(subTabPanel.numChildren-1).y + 49.5;
					}
					subTabPanel.addChild(subTab);
					_subTabViewStack.addBinding(buildTabPanel,subTab);
				}
				
				tab.x = 0;
				if(tabContainer.numChildren > 0)
				{
					tab.x = tabContainer.getChildAt(tabContainer.numChildren-1).x + 100;
				}
//				tab.x = 100*i;
				tabContainer.addChild(tab);
				_tabViewStack.addBinding(subTabPanel,tab);
				if(i==0){tab.group.itemSelectActionStart(tab,true);}
			}

			tabContainer.x = 68.1;
			tabContainer.y = 3.3;
			subTabContainer.x = 9.1;
			subTabContainer.y = 74.7;
			tabPanelContainer.x = 56.1;
			tabPanelContainer.y = 46.7;
			addChild(tabContainer);
			addChild(subTabContainer);
			addChild(tabPanelContainer);
		}

		public function get list():BuildList
		{
			return _list;
		}		
		
		public function refreshPanel():void
		{
			buildTabPanel.update();
		}

		public function set dataArray(value:Array):void
		{
			_dataArray = value;
			buildTabPanel.sourceArray = value;
		}
		
		public function get dataArray():Array
		{
			return _dataArray;
		}

	}
}
import com.darwin.darwinUI.assist.IGroupItem;
import com.darwin.darwinUI.controls.TabPanelBase;

import module.building.view.buildingListPanel.BuildTab;

class SubTabPanel extends TabPanelBase
{
	public function SubTabPanel()
	{		
		super();
	}
	
	/**
	 * 当选项卡被选中后，自动选中其预设的次级选项卡 
	 */
	override public function tabSelectionChange(tab:IGroupItem):void
	{
		(tab as BuildTab).autoSelectSubTab.group.itemSelectActionStart((tab as BuildTab).autoSelectSubTab,true);
	}
}